/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.adapters.impl;

import cz.muni.fi.pa165.bookingmanager.adapters.ReservationAdapter;
import cz.muni.fi.pa165.bookingmanager.dto.HotelDTO;
import cz.muni.fi.pa165.bookingmanager.dto.ReservationDTO;
import cz.muni.fi.pa165.bookingmanager.dto.RoomDTO;
import cz.muni.fi.pa165.bookingmanager.entities.Hotel;
import cz.muni.fi.pa165.bookingmanager.entities.Reservation;
import cz.muni.fi.pa165.bookingmanager.entities.Room;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.exceptions.PreexistingEntityException;
import cz.muni.fi.pa165.bookingmanager.other.PaymentStatusEnum;
import cz.muni.fi.pa165.bookingmanager.services.ReservationService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Jiří Holuša
 */
@Stateless
public class ReservationAdapterImpl implements ReservationAdapter {

    @EJB
    private ReservationService reservationService;

    @Override
    public void createReservation(ReservationDTO reservation) throws IllegalOrphanException, PreexistingEntityException {
        Reservation reservationEntity = AdaptersHelper.reservationDTOToEntity(reservation);
        reservationService.createReservation(reservationEntity);
    }

    @Override
    public void updateReservation(ReservationDTO reservation) throws IllegalOrphanException, NonexistentEntityException {
        Reservation reservationEntity = AdaptersHelper.reservationDTOToEntity(reservation);
        reservationService.updateReservation(reservationEntity);
    }

    @Override
    public void deleteReservation(ReservationDTO reservation) throws IllegalOrphanException, NonexistentEntityException {
        Reservation reservationEntity = AdaptersHelper.reservationDTOToEntity(reservation);
        reservationService.deleteReservation(reservationEntity);
    }

    @Override
    public ReservationDTO getResevationById(Long reservationId) {
        Reservation reservationEntity = reservationService.getResevationByID(reservationId);
        ReservationDTO reservation = AdaptersHelper.reservationEntityToDTO(reservationEntity);
        return reservation;
    }

    @Override
    public List<ReservationDTO> findAllHotelReservations(HotelDTO hotel) {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        List<Reservation> reservationEntities = reservationService.findAllHotelReservations(hotelEntity);

        List<ReservationDTO> reservationDTO = new ArrayList<ReservationDTO>();
        for (Reservation reservationEntity : reservationEntities) {
            reservationDTO.add(AdaptersHelper.reservationEntityToDTO(reservationEntity));
        }

        return reservationDTO;
    }

    @Override
    public List<ReservationDTO> findAllRoomReservations(RoomDTO room) {
        if (room != null) {
            Room roomEntity = AdaptersHelper.roomDTOToEntity(room);

            List<Reservation> reservationEntities = reservationService.findAllRoomReservations(roomEntity);

            List<ReservationDTO> reservationDTO = new ArrayList<ReservationDTO>();
            for (Reservation reservationEntity : reservationEntities) {
                reservationDTO.add(AdaptersHelper.reservationEntityToDTO(reservationEntity));
            }

            return reservationDTO;
        } else {
            return null;
        }
    }

    @Override
    public List<ReservationDTO> findAllHotelReservations(HotelDTO hotel, Date startDate) {
        Hotel hotelEntity = AdaptersHelper.hotelDTOToEntity(hotel);

        List<Reservation> reservationEntities = reservationService.findAllHotelReservations(hotelEntity, startDate);

        List<ReservationDTO> reservationDTO = new ArrayList<ReservationDTO>();
        for (Reservation reservationEntity : reservationEntities) {
            reservationDTO.add(AdaptersHelper.reservationEntityToDTO(reservationEntity));
        }

        return reservationDTO;
    }

    @Override
    public List<ReservationDTO> findAllRoomReservations(RoomDTO room, Date startDate) {
        Room roomEntity = AdaptersHelper.roomDTOToEntity(room);

        List<Reservation> reservationEntities = reservationService.findAllRoomReservations(roomEntity, startDate);

        List<ReservationDTO> reservationDTO = new ArrayList<ReservationDTO>();
        for (Reservation reservationEntity : reservationEntities) {
            reservationDTO.add(AdaptersHelper.reservationEntityToDTO(reservationEntity));
        }

        return reservationDTO;
    }

    @Override
    public List<ReservationDTO> findAllReservationsByPaid(PaymentStatusEnum paid) {
        List<Reservation> reservationEntities = reservationService.findAllReservationsByPaid(paid);

        List<ReservationDTO> reservationDTO = new ArrayList<ReservationDTO>();
        for (Reservation reservationEntity : reservationEntities) {
            reservationDTO.add(AdaptersHelper.reservationEntityToDTO(reservationEntity));
        }

        return reservationDTO;
    }

    public void setCustomerService(ReservationService reservationService) {
        this.reservationService = reservationService;
    }
}
